package com.lx.ngsl.core.role.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.lx.ngsl.core.role.entity.RoleEntity;
import com.lx.ngsl.core.role.repository.RoleRepository;
import com.lx.ngsl.core.user.entity.UserEntity;
import com.lx.ngsl.util.UuidUtil;

@Service
public class RoleService {

	@Autowired
	private RoleRepository repository;
	
	/**
	 * 通过角色id查找角色名称
	 * @Title：getRole 
	 * @param ：@param roleId
	 * @param ：@return 
	 * @return ：String 
	 * @throws
	 */
	public String getRole(String roleId) {
		return repository.findById(roleId).orElse(new RoleEntity()).getName();
	}
	
	/*
	 * 判断角色是否存在
	 */
	public boolean existRole(String roleId) {
		return repository.existsById(roleId);
	}
	
	
	/**
	 * 返回所有角色id和角色名称的对应关系
	 * @Title：roleMap 
	 * @return 
	 * @return ：Map<String,String> 
	 * @throws
	 */
	public Map<String,String> roleMap(){
		Map<String,String> roleMap = new HashMap<>();
		List<RoleEntity> roles = repository.findAll();
		if(roles != null && roles.size() > 0) {
			for(RoleEntity role : roles) {
				roleMap.put(role.getId(), role.getName());
			}
		}
		
		return roleMap;
	}
	
	/**
	 * 查询所有角色信息
	 * @Title：roleList 
	 * @return 
	 * @return ：List<RoleEntity> 
	 * @throws
	 */
	public List<RoleEntity> roleList(){
		return repository.findAll();
	}
	/*
	 * 保存角色
	 */
	public boolean saveRole(RoleEntity role) {
		List<RoleEntity> roles = repository.findByName(role.getName());
		if(roles != null && roles.size() > 0) {
			return false;
		}
		role.setId(UuidUtil.id());
		repository.save(role);
		return true;
	}
	
	/**
	 * 
	 * <p>Method ：roleListPage
	 * <p>Description : 角色分页查询方法
	 *
	 * @param name
	 * @param description
	 * @param pageable
	 * @return
	 * @throws Exception 
	 */
	public Page<RoleEntity> roleListPage(String name,String description, Pageable pageable) throws Exception {
			
			Page<RoleEntity> list = repository.findAll(new Specification<RoleEntity>() {
				@Override
				public Predicate toPredicate(Root<RoleEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
					Predicate predicate = cb.conjunction();
					List<Expression<Boolean>> expressions = predicate.getExpressions();
					if (!StringUtils.isEmpty(name)) {
						expressions.add(cb.like(root.<String> get("name"), "%" + name + "%"));
					}
					if (!StringUtils.isEmpty(description)) {
						expressions.add(cb.like(root.<String> get("description"), "%" + description + "%"));
					}
					return predicate;
				}
			}, pageable);
	
			return list;
		}
}
